package com.allianz.webapps.secondController;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.allianz.frame.utils.StringUtil;
import com.allianz.webapps.common.CustomerContextHolder;
import com.allianz.webapps.https.interfaces.type001.pojo.User;
import com.allianz.webapps.service.AzclPushLogService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


@Controller
@RequestMapping(value="/notices")
public class MyNoticesController {

	@Autowired
	AzclPushLogService   pushLogService;
	@Autowired
	ObjectMapper   obMapper;
	
	
	SimpleDateFormat  sdf  = new SimpleDateFormat("yyyy-MM-dd");
	
	
	@RequestMapping(value="/main")
	public  String   toNoticePage(){
		return "views/page/notices";
	}
	
	@RequestMapping(value="/getAllList")
	@ResponseBody
	public  List<Map<String,Object>> getAllList(@RequestParam(value="jsonParam",required=false)String jsonParam,HttpSession session) throws JsonParseException, JsonMappingException, IOException, ParseException{
			User user = (User) session.getAttribute("username");
			CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_NEWTOUCH);
			List<Map<String,Object>> resultMap =pushLogService.getAllContentByPage(Long.parseLong(user.getUserIntId()));
			for (Map<String, Object> resultMapr : resultMap) {
				String  type=(String) resultMapr.get("PUSH_TYPE");
				String time  = (String) resultMapr.get("PUSH_TIME");
				String  ext = (String) resultMapr.get("PUSH_EXT");
				String contractId =null;
				String policyRef=null;
				if (StringUtil.notNull(ext)) {
					@SuppressWarnings("unchecked")
					Map<String,Object> param=obMapper.readValue(ext, Map.class);
					contractId= (String) param.get("contractId");
					policyRef= (String) param.get("policyRef");
				}
				
				resultMapr.put("PUSH_TIME", sdf.format(sdf.parse(time)));
				if ("101".equals(type)) {
					resultMapr.put("a", "详情");
					resultMapr.put("detail", 1);
					resultMapr.put("displayWord", "新");
					resultMapr.put("displayName", "新业务");
					if (StringUtil.notNull(contractId) && StringUtil.notNull(policyRef)) {
					resultMapr.put("url", "../note/noteContent.action?contractId="+contractId+"&policyRef="+policyRef+"&");
						}else{
						resultMapr.put("url", "../note/noteContent.action?");
					}
				}else 
					if("201".equals(type)){
						resultMapr.put("a", "详情");
						resultMapr.put("style", "info");
						resultMapr.put("detail", 1);
						resultMapr.put("displayWord", "签");
						resultMapr.put("displayName", "电子回执");
						if (StringUtil.notNull(contractId) && StringUtil.notNull(policyRef)) {
							resultMapr.put("url", "../note/eReceipt.action?policyRef="+policyRef+"&contractId="+contractId+"&");
						}else{
							resultMapr.put("url", "../note/eReceipt.action?");
						}
				}else
					if ("301".equals(type) || "302".equals(type)) {
						resultMapr.put("a", "进入查询");
						resultMapr.put("style", "success");
						resultMapr.put("detail", 1);
						resultMapr.put("displayWord", "访");
						resultMapr.put("displayName", "新单回访");
						if (StringUtil.notNull(contractId) && StringUtil.notNull(policyRef)) {
							resultMapr.put("url", "../notes/myNote_3.action?policyRef="+policyRef+"&");
						}else{
							resultMapr.put("url", "../notes/myNote_3.action?");
						}
				}else
					if ("303".equals(type)) {
						resultMapr.put("style", "success");
						resultMapr.put("detail", 0);
						resultMapr.put("displayWord", "访");
						resultMapr.put("displayName", "新单回访");
				}else
					if ("401".equals(type)) {
						resultMapr.put("a", "详情");
						resultMapr.put("style", "success");
						resultMapr.put("detail", 1);
						resultMapr.put("displayWord", "质");
						resultMapr.put("displayName", "质检");
						if (StringUtil.notNull(contractId) && StringUtil.notNull(policyRef)) {
							resultMapr.put("url", "../qualityCheck/noteDetailQC.action?policyRef="+policyRef+"&contractId="+contractId+"&");
						}else{
							resultMapr.put("url", "../qualityCheck/noteDetailQC.action?");
						}
				}else 
					if("501".equals(type) ){
						resultMapr.put("a", "进入查询");
						resultMapr.put("style", "danger");
						resultMapr.put("detail", 1);
						resultMapr.put("displayWord", "变");
						resultMapr.put("displayName", "保全");
						if (StringUtil.notNull(policyRef)) {
							resultMapr.put("url", "../MyPolicy/claimslist.action?policyRef="+policyRef+"&");
						}else{
							resultMapr.put("url", "../MyPolicy/claimslist.action?");
						}
				}else
					if ("502".equals(type)) {
						resultMapr.put("a", "进入查询");
						resultMapr.put("style", "danger");
						resultMapr.put("detail", 1);
						resultMapr.put("displayWord", "变");
						resultMapr.put("displayName", "保全");
						if (StringUtil.notNull(policyRef)) {
							resultMapr.put("url", "../notes/myNote_2.action?policyRef="+policyRef+"&");
						}else{
							resultMapr.put("url", "../notes/myNote_2.action?");
						}
				}else
					if ("503".equals(type)) {
						resultMapr.put("a", "进入查询");
						resultMapr.put("style", "danger");
						resultMapr.put("detail", 1);
						resultMapr.put("displayWord", "变");
						resultMapr.put("displayName", "保全");
						if (StringUtil.notNull(policyRef)) {
							resultMapr.put("url", "../MyPolicy/claimslist.action?policyRef="+policyRef+"&");
						}else{
							resultMapr.put("url", "../MyPolicy/claimslist.action?"); 
						}
				}else
					if ( "602".equals(type)) {
						resultMapr.put("a", "进入查询");
						resultMapr.put("style", "primary");
						resultMapr.put("detail", 1);
						resultMapr.put("displayWord", "赔");
						resultMapr.put("displayName", "理赔");
						if (StringUtil.notNull(policyRef)) {
							resultMapr.put("url", "../MyPolicy/preservation.action?policyRef="+policyRef+"&");
						}else{
							resultMapr.put("url", "../MyPolicy/preservation.action?"); 
						}
				}else
					if ("603".equals(type)) {
						resultMapr.put("a", "进入查询");
						resultMapr.put("style", "primary");
						resultMapr.put("detail", 1);
						resultMapr.put("displayWord", "赔");
						resultMapr.put("displayName", "理赔");
						if (StringUtil.notNull(policyRef)) {
							resultMapr.put("url", "../notes/myNote_4.action?policyRef="+policyRef+"&");
						}else{
							resultMapr.put("url", "../notes/myNote_4.action?"); 
						}
				}else
					if ("604".equals(type)) {
						resultMapr.put("a", "进入查询");
						resultMapr.put("style", "primary");
						resultMapr.put("detail", 1);
						resultMapr.put("displayWord", "赔");
						resultMapr.put("displayName", "理赔");
						if (StringUtil.notNull(policyRef)) {
							resultMapr.put("url", "../MyPolicy/preserlist.action?policyRef="+policyRef+"&");
						}else{
							resultMapr.put("url", "../MyPolicy/preserlist.action?"); 
						}
				}else 
					if ("605".equals(type)){
					//2017-4-6 add
					resultMapr.put("style", "primary");
					resultMapr.put("detail", 0);
					resultMapr.put("displayWord", "赔");
					resultMapr.put("displayName", "理赔");	
				}else
					if ("601".equals(type)) {
						resultMapr.put("style", "primary");
						resultMapr.put("detail", 0);
						resultMapr.put("displayWord", "赔");
						resultMapr.put("displayName", "理赔");	
				}else
						if ("701".equals(type) || "702".equals(type)) {
							resultMapr.put("a", "详情");
							resultMapr.put("style", "warning");
							resultMapr.put("detail", 1);
							resultMapr.put("displayWord", "续");
							resultMapr.put("displayName", "续期");
							if (StringUtil.notNull(policyRef)) {
								resultMapr.put("url", "../MyPolicy/renewal.action?policyRef="+policyRef+"&");
							}else{
								resultMapr.put("url", "../MyPolicy/renewal.action?"); 
							}
					}else
						if ( "703".equals(type)) {
						resultMapr.put("style", "warning");
						resultMapr.put("detail", 0);
						resultMapr.put("displayWord", "续");
						resultMapr.put("displayName", "续期");
					}
				resultMapr.remove("RN");
				resultMapr.remove("DEVICE_TYPE");
				resultMapr.remove("CHANNEL_ID");
				resultMapr.remove("PUSH_RESULT_ID");
			}
			return resultMap;
	}
	/**
	 * 查询用户未读消息数量
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/unReadCount")
	@ResponseBody
	public  int getUnRead(HttpSession session){
		User user = (User) session.getAttribute("username");
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_NEWTOUCH);
		return pushLogService.getUNReadCount(user.getUserIntId());
	}
	
	/**
	 * 标记已读
	 * @return
	 */
	@RequestMapping(value="/readDone")
	@ResponseBody
	public Map<String,Object>  readCon(HttpSession session,String id){
		Map<String,Object>  map = new HashMap<String,Object>();
		String pushId=null;
		pushId=id;
		try{
			User user = (User) session.getAttribute("username");
			String agentId = user.getUserIntId();
			CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_NEWTOUCH);
			pushLogService.readByPushId(agentId,pushId);
		}catch (Exception e){
			map.put("status", "success");
		}
		map.put("status", "success");
		return  map;
	}
}
